<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of prufer</title>
  <meta name="keywords" content="prufer">
  <meta name="description" content="prufer --- convert a tree to/from its Prufer code">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../index.html">Home</a> &gt;  <a href="../index.html">matgraph</a> &gt; <a href="index.html">@graph</a> &gt; prufer.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for matgraph/@graph&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>prufer
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>prufer --- convert a tree to/from its Prufer code</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function output = prufer(g, code) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> prufer --- convert a tree to/from its Prufer code
 output = prufer(g) returns the Prufer code for g (assuming g is a tree)
 prufer(g,code) overwrites g with a tree based on the code 

 The Prufer code is a way to map bijectively trees on n vertices 
 into n-2 long sequences of integers drawn from [n].</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="add.html" class="code" title="function add(g,i,j)">add</a>	add --- add edge(s) to the graph</li><li><a href="copy.html" class="code" title="function copy(g,h)">copy</a>	copy(g,h) --- overwrite g with a copy of h</li><li><a href="deg.html" class="code" title="function d = deg(g,v)">deg</a>	deg: degree of a vertex or degree sequence</li><li><a href="delete.html" class="code" title="function delete(g,i,j)">delete</a>	delete --- delete vertices or edges from a graph</li><li><a href="edges.html" class="code" title="function elist = edges(g)">edges</a>	edges(g) --- list the edges in g as a 2-column matrix</li><li><a href="free.html" class="code" title="function free(g)">free</a>	free(g) --- free the graph from the system</li><li><a href="graph.html" class="code" title="function g = graph(n)">graph</a>	graph: constructor for the graph class</li><li><a href="isconnected.html" class="code" title="function yn = isconnected(g)">isconnected</a>	isconnected(g) --- test if g is a connected graph</li><li><a href="ne.html" class="code" title="function m = ne(g,h)">ne</a>	ne(g) --- number of edges in g or ne(g,h) --- check for inequality</li><li><a href="neighbors.html" class="code" title="function nlist = neighbors(g,v)">neighbors</a>	neighbors(g,v) --- neighbors of v as a list.</li><li><a href="nv.html" class="code" title="function n = nv(g)">nv</a>	nv(g) --- number of vertices in g</li><li><a href="resize.html" class="code" title="function resize(g, n)">resize</a>	resize(g,n) --- change the number of vertices in g to n</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->

<h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="#_sub1" class="code">function ok = prufer_check(list)</a></li></ul>
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function output = prufer(g, code)</a>
0002 <span class="comment">% prufer --- convert a tree to/from its Prufer code</span>
0003 <span class="comment">% output = prufer(g) returns the Prufer code for g (assuming g is a tree)</span>
0004 <span class="comment">% prufer(g,code) overwrites g with a tree based on the code</span>
0005 <span class="comment">%</span>
0006 <span class="comment">% The Prufer code is a way to map bijectively trees on n vertices</span>
0007 <span class="comment">% into n-2 long sequences of integers drawn from [n].</span>
0008 
0009 <span class="keyword">if</span> nargin==1
0010     n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0011     m = <a href="ne.html" class="code" title="function m = ne(g,h)">ne</a>(g);
0012     <span class="keyword">if</span> (m ~= n-1)
0013         error(<span class="string">'Input graph is not a tree'</span>)
0014     <span class="keyword">end</span>
0015     <span class="keyword">if</span> (~<a href="isconnected.html" class="code" title="function yn = isconnected(g)">isconnected</a>(g))
0016         error(<span class="string">'Input graph is not a tree'</span>)
0017     <span class="keyword">end</span>
0018     
0019     <span class="keyword">if</span> (n &lt; 2)
0020         error(<span class="string">'Algorithm applies only to trees with 2 or more vertices'</span>)
0021     <span class="keyword">end</span>
0022     
0023     <span class="keyword">if</span> (n==2)
0024         output=[];
0025         <span class="keyword">return</span>
0026     <span class="keyword">end</span>
0027     
0028     t = <a href="graph.html" class="code" title="function g = graph(n)">graph</a>;  <span class="comment">% temporary copy of g</span>
0029     <a href="copy.html" class="code" title="function copy(g,h)">copy</a>(t,g);
0030     
0031     names = 1:n;  <span class="comment">% names of the vertices</span>
0032     output = zeros(1,n-2);
0033     
0034     <span class="keyword">for</span> i=1:n-2
0035         <span class="comment">% find least leaf</span>
0036         d = <a href="deg.html" class="code" title="function d = deg(g,v)">deg</a>(t);
0037         v = min(find(d==1));
0038         
0039         <span class="comment">% find neighbor of the least leaf</span>
0040         w = <a href="neighbors.html" class="code" title="function nlist = neighbors(g,v)">neighbors</a>(t,v);
0041         
0042         output(i) = names(w); <span class="comment">% add this vertex to the output</span>
0043         
0044         <span class="comment">% delete v and its name</span>
0045         <a href="delete.html" class="code" title="function delete(g,i,j)">delete</a>(t,v);
0046         names = [names(1:v-1),names(v+1:end)];
0047     <span class="keyword">end</span>
0048         
0049     <a href="free.html" class="code" title="function free(g)">free</a>(t)
0050     <span class="keyword">return</span>
0051 <span class="keyword">end</span>
0052 
0053 <span class="keyword">if</span> ~<a href="#_sub1" class="code" title="subfunction ok = prufer_check(list)  ">prufer_check</a>(code)
0054     error(<span class="string">'Input is not a valid Prufer code'</span>)
0055 <span class="keyword">end</span>
0056 
0057 n = length(code) + 2;
0058 
0059 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,0);
0060 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,n);
0061 
0062 verts = 1:n;
0063 <a href="edges.html" class="code" title="function elist = edges(g)">edges</a> = zeros(n-1,2);
0064 
0065 <span class="keyword">for</span> k=1:n-2
0066     u = min(setdiff(verts,code));
0067     v = code(1);
0068     e = [u,v];
0069     <a href="edges.html" class="code" title="function elist = edges(g)">edges</a>(k,:) = e;
0070     code = code(2:end);
0071     i = find(verts == u);
0072     verts = [verts(1:i-1),verts(i+1:end)];
0073 <span class="keyword">end</span>
0074 
0075 
0076 <a href="edges.html" class="code" title="function elist = edges(g)">edges</a>(n-1,:) = verts;
0077 <a href="add.html" class="code" title="function add(g,i,j)">add</a>(g,<a href="edges.html" class="code" title="function elist = edges(g)">edges</a>);
0078     
0079 
0080 
0081 
0082 <a name="_sub1" href="#_subfunctions" class="code">function ok = prufer_check(list)  </a><span class="comment">% check if list is a valid Prufer code</span>
0083 ok = false;
0084 n = length(list)+2;
0085 
0086 <span class="keyword">if</span> (any(list &gt; n)) || (any(list &lt; 1)) || (any(list ~= round(list)))
0087     <span class="keyword">return</span>
0088 <span class="keyword">end</span>
0089 
0090 ok = true;
0091 
0092</pre></div>
<hr><address>Generated on Fri 30-Apr-2010 07:51:16 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>